//
//  LYPlaceHolderTextView.m
//  LYProfiles
//
//  Created by boom on 2017/6/9.
//  Copyright © 2017年 boomLi. All rights reserved.
//

#import "LYPlaceHolderTextView.h"
#import "UIView+Extension.h"

@interface LYPlaceHolderTextView()

@property (nonatomic,weak)UILabel *placeholderLabel;

@end

@implementation LYPlaceHolderTextView

-(void)awakeFromNib{
    [super awakeFromNib];
    
    UILabel *placeholderLabel = [[UILabel alloc]init];
    //    placeholderLabel.frame = self.bounds;
    placeholderLabel.ftn_x = 20;
    placeholderLabel.ftn_y = 30;
    placeholderLabel.textColor = [UIColor grayColor];
    placeholderLabel.numberOfLines = 0;
    placeholderLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:placeholderLabel];
    self.placeholderLabel = placeholderLabel;
    
    /** 监听输入的通知 */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        UILabel *placeholderLabel = [[UILabel alloc]init];
        placeholderLabel.frame = self.bounds;
        placeholderLabel.ftn_x = 20;
        placeholderLabel.ftn_y = 7;
        placeholderLabel.textColor = [UIColor grayColor];
        placeholderLabel.numberOfLines = 0;
        placeholderLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:placeholderLabel];
        self.placeholderLabel = placeholderLabel;
        
        /** 监听输入的通知 */
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)textChange{
    
    
    self.placeholderLabel.hidden = self.hasText;
}

-(void)setPlaceholderText:(NSString *)placeholderText{
    
    _placeholderText = [placeholderText copy];
    
    self.placeholderLabel.text = placeholderText;
    self.placeholderLabel.frame = self.bounds;
    [self setNeedsLayout];
}

-(void)setPlaceholderColor:(UIColor *)placeholderColor{
    
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
    [self setNeedsLayout];
}

-(void)setText:(NSString *)text{
    
    
    [super setText:text];
    [self textChange];
    
}
-(void)setTextColor:(UIColor *)textColor{
    
    [super setTextColor:textColor];
}

-(void)setFont:(UIFont *)font{
    
    [super setFont:font];
    
    self.placeholderLabel.font = font;
    [self setNeedsLayout];
}


-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    //从XIb过来的情况下，x值发生了改变
    //    self.placeholderLabel.ftn_x = 20;
    self.placeholderLabel.ftn_y = 10;
    self.placeholderLabel.ftn_width = self.ftn_width - self.placeholderLabel.ftn_x * 2;
    self.placeholderLabel.ftn_x = 7;
    [self.placeholderLabel sizeToFit];
}


@end
